package org.huangrui.javase.collectionMap.collection;

import org.junit.Test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;

/**
 * @Author hr
 * @Create 2025-11-07 15:34
 **/
public class T03List {
    @Test
    public void test1(){
        long start = System.currentTimeMillis();
        ArrayList<Integer> list = new ArrayList<>();
        for(int i=1; i<=100000; i++){
            list.add(i-1, i);
        }
        long end = System.currentTimeMillis();
        System.out.println("ArrayList末尾位置添加时间：" + (end-start));
    }

    @Test
    public void test2(){
        long start = System.currentTimeMillis();
        LinkedList<Integer> list = new LinkedList<>();
        for(int i=1; i<=100000; i++){
            list.add(i-1, i);
        }
        long end = System.currentTimeMillis();
        System.out.println("LinkedList末尾位置添加时间：" + (end-start));
    }
    @Test
    public void test3(){
        long start = System.currentTimeMillis();
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);//这里先添加1个，保证list.size()不为0
        Random random = new Random();
        for(int i=1; i<=100000; i++){
            int index = random.nextInt(0, list.size());
            list.add(index, i);
        }
        long end = System.currentTimeMillis();
        System.out.println("ArrayList任意位置添加时间：" + (end-start));
    }

    @Test
    public void test4(){
        long start = System.currentTimeMillis();
        LinkedList<Integer> list = new LinkedList<>();
        list.add(1);//这里先添加1个，保证list.size()不为0
        Random random = new Random();
        for(int i=1; i<=100000; i++){
            int index = random.nextInt(0, list.size());
            list.add(index, i);
        }
        long end = System.currentTimeMillis();
        System.out.println("LinkedList任意位置添加时间：" + (end-start));
    }
}
